import React, { Component } from 'react'
import './index.css'

export default class Footer extends Component {

  // 全选or取消全选回调
  handleCheckAll = (event)=>{
    this.props.checkAllTodo(event.target.checked)
  }

  //清除已完成任务
  handleClearAllDone = ()=>{
    this.props.ClearAllDone()
  }

  render() {
    const { todos } = this.props
    //已完成个数,总数
    const doneCount = todos.reduce((sum,todo)=>{ return sum += todo.done ? 1 : 0 },0)
    const total = todos.length
    return (
      <div className="todo-footer">
            <label>
              {/* defaultChecked只能是第一次时候生效 */}
              {/* <input type="checkbox" defaultchecked={doneCount === total ? true : false}/> */}
              {/* checked能指定多次,始终以你最后一次指定为主 */}
              <input type="checkbox" onChange={this.handleCheckAll} checked={doneCount === total && total !== 0 ? true : false} />
            </label>
            <span>
              <span>已完成{doneCount}</span> / 全部{total}
            </span>
            <button className="btn btn-danger" onClick={this.handleClearAllDone}>清除已完成任务</button>
      </div>
    )
  }
}
